package com.party.mobile.biz.wechatPlatform;

import com.google.common.base.Charsets;
import com.party.common.constant.WechatConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 微信开放平台业务接口
 * Created by wei.li
 *
 * @date 2017/8/26 0026
 * @time 15:25
 */

@Service
public class WechatPlatformBizService {

    @Value("#{wechat_platform['wechat.platform.appId']}")
    private String componentAppid;

    @Value("#{wechat_platform['wechat.platform.appSecret']}")
    private String componentAppsecret;

    private static final String APPID = "APPID";

    private static final String REDIRECT_URI = "REDIRECT_URI";

    private static final String COMPONENT_APPID = "COMPONENT_APPID";

    /**
     * 获取授权url
     * @param redirectUri 重定向
     * @param appid appid
     * @return url
     * @throws UnsupportedEncodingException
     */
    public String getOauth2Url(String redirectUri, String appid) throws UnsupportedEncodingException {
        redirectUri = URLEncoder.encode(redirectUri, Charsets.UTF_8.name());
        return  WechatConstant.OAUTH2_URL
                .replace(COMPONENT_APPID, componentAppid)
                .replace(APPID, appid)
                .replace(REDIRECT_URI, redirectUri);
    }
}
